{include file="public/header" /}
<style>
    ul>li {
        width:auto !important;
    }

</style>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>{$Think.lang.course_list}</h3>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>

    <form id="goods_class_form" enctype="multipart/form-data" method="post" action='course_edit'>
        <table class="ds-default-table">
            <tbody>
                <tr class="noborder">
                    <td colspan="2" class="required w120"><label class="validation" >主教程名称:</label></td>
                    <td class="vatop rowform"><input type="text" value="{$course_info.title}" name="title" id="title" maxlength="20" class="txt"></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation" >{$Think.lang.goods_class_add_type}:</label></td>
                    <td id="gcategorys">  <div style="margin-left: 20px;">
                        {$course_info.gc_name} <input type="button" value="编辑" id="gc_edit">
                        <input name="gc_ids" value="1"  type="hidden" id="gc_ids">
                    </div></td>
                    <td colspan="2" id="gcategory" style="display:none;" ><select class="class-select" name ='cat_id' id='cat_id' >
                        <option value="0">{$Think.lang.ds_please_choose}...</option>
                        {notempty name="gc_list"}
                        {foreach name="gc_list" item="v" key="k" }
                        <option value="{$v.gc_id}">{$v.gc_name}</option>
                        {/foreach}
                        {/notempty}
                    </select>
                    </td>
                </tr>

                <tr>
                    <td colspan="2" class="required"><label class="validation" >{$Think.lang.course_brand}:</label></td>
                    <td class="vatop">
                        <div  id="ajax_brand" class="dssc-brand-select w180">
                            <div class="selection">
                                <input name="b_name" id="b_name" value="{$course_info.brand_name|default=''}" type="text" class="txt w180" readonly="readonly" />
                                <input type="hidden" name="b_id" id="b_id" value="{$course_info.brand_id|default=''}" />
                            </div>
                            <div class="dssc-brand-select-container">
                                <div class="brand-index" style="width: 210px;padding-bottom: 10px;margin: 6px auto;border-bottom: dotted 1px #CCC;" data-url="{:url('Common/ajax_get_brand')}">
                                    <div class="letter" dstype="letter">
                                        <ul style="width: 100%;" >
                                            <li style="width: auto;" ><a href="javascript:void(0);" data-letter="all">所有品牌</a></li>
                                            <li style="width: auto;" ><a href="javascript:void(0);" data-letter="A">A</a></li>
                                            <li  style="width: auto;" ><a href="javascript:void(0);" data-letter="B">B</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="C">C</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="D">D</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="E">E</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="F">F</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="G">G</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="H">H</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="I">I</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="J">J</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="K">K</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="L">L</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="M">M</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="N">N</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="O">O</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="P">P</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Q">Q</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="R">R</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="S">S</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="T">T</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="U">U</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="V">V</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="W">W</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="X">X</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Y">Y</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Z">Z</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="0-9">其他</a></li>
                                        </ul>
                                    </div>
                                    <div class="search" dstype="search"><input name="search_brand_keyword" id="search_brand_keyword" type="text" class="text" /><a href="javascript:void(0);" class="dssc-btn-mini" style="vertical-align: top;">Go</a></div>
                                </div>
                                <div class="brand-list" dstype="brandList">
                                    <ul dstype="brand_list">
                                        {notempty name="brand_list"}
                                        {foreach name="brand_list" item="val"}
                                        <li data-id='{$val.brand_id}'data-name='{$val.brand_name}'><em>{$val.brand_initial}</em>{$val.brand_name}</li>
                                        {/foreach}
                                        {/notempty}
                                    </ul>
                                </div>
                                <div class="no-result" dstype="noBrandList" style="display: none;">{$Think.lang.no_brand_result}</div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation">教程类型:</label></td>
                    <td class="vatop">
                        <select class="class-select" name='type'>
                            <option value="1"  {if isset($course_info['type']) &&  $course_info['type'] == 1 }selected{/if} >综合</option>
                            <option value="2" {if isset($course_info['type']) &&  $course_info['type'] == 2 }selected{/if} >教程</option>
                            <option value="3" {if isset($course_info['type']) &&  $course_info['type'] == 3 }selected{/if} >技术资料</option>
                        </select>
                    </td>
                </tr>

                 <tr>
                    <td colspan="2" class="required"><label class="validation" >{$Think.lang.goods_class_add_img}:</label></td>

                     <td class="vatop rowform">
                        <span class="type-file-box">
                           <input type="hidden" value="{$course_info.img}" name="pic" id="upload">
                            <input name="img" type="file"  id="upload_img" size="30" hidefocus="true" >
                        </span>
                     </td>
                     <td class="vatop rowform"  id="show_img" >
                         <img src="{$url}" style="width:200px;height: 300px"/>
                     </td>
                     <td>建议图片大小：375*224</td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation" >{$Think.lang.course_status}:</label></td>
                    <td class="vatop rowform">
                        <div class="onoff">
                            <label for="site_state1" class="cb-enable {if condition="$course_info.status eq 0"}selected{/if}" >启用</label>
                            <label for="site_state0" class="cb-disable {if condition="$course_info.status eq 1"}selected{/if}">停用</label>
                            <input id="site_state1" name="status" value="0" type="radio" {if condition="$course_info.status eq 0"} checked="checked"{/if}>
                            <input id="site_state0" name="status" value="1" type="radio" {if condition="$course_info.status eq 1"} checked="checked"{/if}>
                        </div>
                    </td>
                </tr>
                 <tr>
                    <td colspan="2" class="required"><label class="validation" >{$Think.lang.goods_class_add_content}:</label></td>
                     <td colspan="2" >
                         <textarea name="content" id="content" cols="40" rows="150">{$course_info.content}</textarea>
                     </td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation" >是否热门:</label></td>
                    <td class="vatop rowform">
                        <div class="onoff">
                            <label class="cb-enable {if condition="$course_info.popular eq 1"}selected{/if}" >是</label>
                            <label class="cb-disable {if condition="$course_info.popular eq 0"}selected{/if}">否</label>
                            <input name="popular" value="1" type="radio" {if condition="$course_info.popular eq 1"} checked="checked"{/if}>
                            <input name="popular" value="0" type="radio" {if condition="$course_info.popular eq 0"} checked="checked"{/if}>
                        </div>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2"><input class="btn" type="button" value="{$Think.lang.ds_submit}" id='aa'/></td>
                    <input class="cat_id1" type="hidden" name='cat_id1' value="{$course_info.cat_id1}" >
                    <input class="cat_id2" type="hidden" name='cat_id2' value="{$course_info.cat_id2}" >
                    <input class="course_id" type="hidden" name='course_id' value="{$course_info.id}" >
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script>
//按钮先执行验证再提交表单
              $(function() {
                  /*编辑分类*/
                  $('#gc_edit').click(function () {
                      $('#gcategorys').remove();
                      $('#gcategory').show();
                  });


                  //品牌
                  $("#ajax_brand").brandinit();

                  $('#dssubmit').click(function(){
                      $('#formSearch').submit();
                  });

                  $('#dssubmit').click(function(){
                      $('#formSearch').submit();
                  });

                  $('#type_div').perfectScrollbar();
                  $("#pic").change(function() {
                      $("#textfield1").val($(this).val());
                  });
                  $('input[type="radio"][name="cat_id"]').click(function() {
                      if ($(this).val() == '0') {
                          $('#t_name').val('');
                      } else {
                          $('#t_name').val($(this).next('span').html());
                      }
                  });
                  // 所属分类
                  $(document).off('change',"#gc_parent_id").on("change","#gc_parent_id", function() {
                      type_scroll($(this));
                  });
                  // 类型搜索
                  $(document).off('change',"#gcategory > select").on("change","#gcategory > select", function() {
                      type_scroll($(this));
                  });

                  //图片上传
                  $(document).on('change','#upload_img',function() {
                      var files = document.getElementById('upload_img').files[0];
                      var formData = new FormData();
                      formData.append('file',files);
                      layer.msg('正在上传，请稍后');
                      $.ajax({
                          //请求方式
                          type : "POST",
                          //请求地址
                          url : "ajax_upload",
                          data : formData,
                          cache:false,  //默认是true，但是一般不做缓存
                          processData:false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                          contentType:false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                          //请求成功
                          success : function(result) {
                              if(result.err==0){
                                 $('#show_img').remove();
                                  layer.msg(result.msg);
                                  $('#upload').val(result.data);
                              }else{
                                  $('#upload_img').val('');
                                  alert('文件信息错误,请重新上传');return false;
                              }



                          },
                          //请求失败，包含具体的错误信息
                          error : function(e){
                              console.log(e.status);
                              console.log(e.responseText);
                          }
                      });

                  });



                   $('.btn').click(function(){
                      var title = $("#title").val();
                      var status=$('input:radio:checked').val();
                      var brand_id = $("#brand_id").val();
                      var content = $("#content").val();
                      var cat_id = $("#cat_id").val();
                      var cat_id1 =$("#cat_id").next().val();
                      var cat_id2 =$("#cat_id").next().next().val();
                       var gc_ids =$("#gc_ids").val();
                      // 判断
                      if(title.length == ''){
                          layer.msg('请输入教程标题');
                          return false;
                      }
                      if (brand_id == 0) {
                          layer.msg('请选择教程品牌');
                          return;
                      }
                      if(gc_ids!='1'){
                          if (cat_id == 0) {
                              layer.msg('请选择教程分类');
                              return;
                          }
                          if (cat_id1 == '请选择默认') {
                              layer.msg('请选择教程分类');
                              return;
                          }
                          if (cat_id2 == '请选择默认') {
                              layer.msg('请选择教程分类');
                              return;
                          }
                      }

                      if ( content.length ==0) {
                          layer.msg('描述不能为空');
                          return;
                      }
                      if ( content.length >200) {
                          layer.msg('描述需要在200字以内');
                          return;
                      }
                      $('.cat_id1').val(cat_id1);
                      $('.cat_id2').val(cat_id2);
                       var url = $('form').attr('action');
                       $.ajax({
                           type: "POST",
                           url:url,
                           data:$('#goods_class_form').serialize(),// 你的formid

                           error: function(request) {
                               alert("Connection error");
                           },
                           success: function(data) {
                            //aletr(data);return false;
                               if(data.code==0){

                                   layer.msg(data.msg);
                                   location.href=data.url;return false;
                               }else{
                                   layer.msg(data.msg);
                                   location.href=data.url;return false;

                               }
                           }
                       }); return false;
                   });
                });

              var typeScroll = 0;
              function type_scroll(o) {
                  var id = o.val();
                  if (!$('#type_dt_' + id).is('dt')) {
                      return false;
                  }
                  $('#type_div').scrollTop(-typeScroll);
                  var sp_top = $('#type_dt_' + id).offset().top;
                  var div_top = $('#type_div').offset().top;
                  $('#type_div').scrollTop(sp_top - div_top);
                  typeScroll = sp_top - div_top;
              }
              gcategoryInit('gcategory');
</script>


